package com.topsoft.email.action.websocket;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class WebSocketController {

	@Autowired
	private SimpMessagingTemplate template;



	

	@MessageMapping("/greeting")
	@SendTo("/topic/greetings")
	public String handle(String greeting) {
		System.out.println("sendTO"+greeting);
		return "11111: " + greeting;
	}

	
	
	
	@SubscribeMapping("/init")
	public String init() {
		return "init";
	}

	
/**
 * 打招呼
 * @param greeting
 * @throws IOException 
 */
	@RequestMapping(path = "/greetings.action", method={ RequestMethod.GET })
	public void greet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String text = "[222222222222222]:";
		template.convertAndSend("/topic/greetings", text);
		template.convertAndSend("/app/greetings", text);
		response.getWriter().append(text);
		
	}

}